home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 4.1 KB | 159 lines | [TEXT/CWIE] |
- // Key.cp
-
- #ifndef Key_h
- #include "Key.h"
- #endif
- #ifndef Assert_h
- #include "Assert.h"
- #endif
-
- Key::Key( const EventRecord& event )
- : code( KeyCode( Byte1( event.message ) ) ),
- character( Byte0( event.message ) )
- {
- Normalize();
- CheckCharacter();
- if ( (event.modifiers & cmdKey) != 0 )
- CheckForStop();
- }
-
- void Key::Normalize()
- {
- if ( character >= 0x20 )
- return;
-
- static const uint8 firstAlternate[ 0x20 ] =
- {
- 0, 0, 0,0x31, 0, 0, 0, 0, 0, 0, 0, 0, 0,0x2a, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x3b,0x3c,0x3e,0x3d
- };
-
- static const uint8 secondAlternate[ 0x20 ] =
- {
- 0, 0, 0,0x34, 0, 0, 0, 0, 0, 0, 0, 0, 0,0x2a, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x46,0x42,0x4d,0x48
- };
-
- static const uint8 correct[ 0x20 ] =
- {
- 0, 0, 0,0x4c, 0, 0, 0, 0, 0, 0, 0, 0, 0,0x24, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7b,0x7c,0x7e,0x7d
- };
-
- if ( firstAlternate[ character ] == 0 )
- return;
-
- if ( code == firstAlternate[ character ] || code == secondAlternate[ character ] )
- code = KeyCode( correct[ character ] );
- }
-
- void Key::CheckCharacter()
- {
- static const uint8 checks[ 0x80 ] =
- {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,0x0d, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0x09, 0, 0,0x08, 0,0x1b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-
- 0, 0, 0, 0, 0, 0, 0,0x1b, 0, 0, 0, 0, 0x03, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0x1c, 0x1d,0x1f,0x1e, 0,
- };
-
- if ( character >= 0x80 )
- return;
-
- if ( checks[ code ] != 0 && checks[ code ] != character )
- code = unknown;
- }
-
- bool Key::Special() const
- {
- static const uint8 specials[ maxuint8 + 1 ] =
- {
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0,
- 1,0,0,1, 0,1,0,0, 0,0,0,0, 0,0,0,0,
-
- 0,0,0,0, 0,0,0,1, 0,0,0,0, 1,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 1,1,1,1, 1,1,0,1, 0,1,0,1, 0,1,0,1,
- 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
-
- 1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
-
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
- 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
- };
-
- return specials[ code ];
- }
-
- uint16 Key::WithModifiers( EventModifiers modifiers, KCHRCache *kchr ) const
- {
- uint32 state = 0;
- uint32 result = KeyTranslate( kchr,
- code | ( modifiers & 0xff00 ),
- &state );
-
- return (uint16( Byte3( result ) ) << 8) | uint16( Byte0( result ) );
- }
-
- KCHRCache *Key::GetKCHRCache()
- {
- return reinterpret_cast<KCHRCache *>( GetScriptManagerVariable( smKCHRCache ) );
- }
-
- void Key::CheckForStop()
- {
- if ( character == '.' )
- {
- code = stopKey;
- return;
- }
-
- KCHRCache * kchr = GetKCHRCache();
-
- if ( Byte0( WithModifiers( 0, kchr ) ) == '.'
- || Byte0( WithModifiers( shiftKey, kchr ) ) == '.' )
- code = stopKey;
- }
-
- uint16 Key::WithModifiers( EventModifiers modifiers ) const
- {
- return WithModifiers( modifiers, GetKCHRCache() );
- }
-
-
- const uint8 Key::functionNumbers[ 0x20 ] =
- {
- 5,6,7,3, 8,9,0,11, 0,13,0,14, 0,10,0,12,
- 0,15,0,0, 0,0,4,0, 2,0,1,0, 0,0,0,0
- };
-
- bool Key::IsFunctionKey() const
- {
- if ( code < 0x60 || code >= 0x80 )
- return false;
-
- return functionNumbers[ code - 0x60 ] != 0;
- }
-
- uint32 Key::FunctionNumber() const
- {
- Assert( IsFunctionKey() );
-
- Assert( code >= 0x60 );
- Assert( code < 0x80 );
-
- return functionNumbers[ code - 0x60 ];
- }
-